package com.clean.notification.device;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;

import com.clean.notification.BuildConfig;

import java.io.IOException;
import java.net.URLEncoder;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author dongdong.huang
 * @Description:
 * @date 2017/3/6 16:56
 * @copyright TCL-MIG
 */

public class CommonHeaderInterceptorPhone implements Interceptor {
    private static final long DEFAULT_ONLINE_TIMEOUT = 60;  //60秒
    private static final long DEFAULT_OFFLINE_TIMEOUT = 60 * 60 * 24 * 28;  //一个月
    private Context mContext;

    //space+默认不进行压缩，鹰眼api时候需要设置为true
    private boolean isGzipEncode;

    public CommonHeaderInterceptorPhone(final Context context) {
        this(context, DEFAULT_ONLINE_TIMEOUT, DEFAULT_OFFLINE_TIMEOUT);
    }

    public CommonHeaderInterceptorPhone(final Context context, boolean gzipEncode) {
        this(context);
        isGzipEncode = gzipEncode;
    }

    /**
     * @param onlineTimeout  有网络时的缓存有效时间
     * @param offlineTimeout 无网络时的缓存有效时间
     */
    public CommonHeaderInterceptorPhone(final Context context, final long onlineTimeout, final long offlineTimeout) {
        mContext = context;

    }

    public static Boolean isNetworkReachable(final Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo current = cm.getActiveNetworkInfo();
        if (current == null) {
            return false;
        }
        return (current.isAvailable());
    }


    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request.Builder builder = request.newBuilder();

        //String commonParameter = new Gson().toJson(getBasicParams(context));
        //builder.addHeader("commonParameter", commonParameter);
        builder.addHeader("Content-Type", "application/json");
        builder.addHeader("Accept-Type", "application/json");
        builder.addHeader("DeviceId", DeviceInfoPhone.getInstance().getDeviceId() == null ? "" : DeviceInfoPhone.getInstance().getDeviceId());
        builder.addHeader("OperId", "2");
        builder.header("Platform", "android");
        builder.header("ClientId", "app").header("AppVersion", LTRepositoryPhone.getVersion()).header("Channel", LTRepositoryPhone.getChannel()).header("ApiLevel", String.valueOf(LTRepositoryPhone.API_LEVEL)).header("sdkInt", String.valueOf(Build.VERSION.SDK_INT)).header("package", LTRepositoryPhone.getPackageName()).header("debug", BuildConfig.DEBUG ? "true" : "false").header("netType", "unkown").header("model", toURLEncoded(Build.MODEL)).header("brand", toURLEncoded(Build.BRAND)).header("country", (LTRepositoryPhone.getCountry() == null ? "" : LTRepositoryPhone.getCountry())).header("mcc", String.valueOf(mContext.getResources().getConfiguration().mcc));

        if (isGzipEncode) {
            builder.addHeader("Content-Encoding", "gzip");
        }
        request = builder.build();
        Response response = chain.proceed(request);
        return response;
    }


    public static String toURLEncoded(String paramString) {
        if (paramString != null && !paramString.equals("")) {
            try {
                String str = new String(paramString.getBytes(), "UTF-8");
                str = URLEncoder.encode(str, "UTF-8");
                return str;
            } catch (Exception var2) {
                var2.printStackTrace();
                return "";
            }
        } else {
            return "";
        }
    }

    private String checkValue(String value) {
        if (value == null) {
            return "";
        }
        for (int i = 0, length = value.length(); i < length; i++) {
            char c = value.charAt(i);
            if (c <= '\u001f' || c >= '\u007f') {
                return "";
            }
        }
        return value;
    }

}
